import requests

def search(key,pagesize):
    try:
        headers = {
            "authority": "cn.bing.com",
            "accept": "*/*",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "no-cache",
            "pragma": "no-cache",
            "referer": "https://cn.bing.com/images/search?view=detailV2&ccid=I6lvbqPz&id=23A751039A53F5A3AA461FD41C5E0047200DF5E0&thid=OIP.I6lvbqPzQV-6gSYXlmK1OwHaKe&mediaurl=https%3a%2f%2fts1.cn.mm.bing.net%2fth%2fid%2fR-C.23a96f6ea3f3415fba8126179662b53b%3frik%3d4PUNIEcAXhzUHw%26riu%3dhttp%253a%252f%252fpic.baike.soso.com%252fp%252f20140221%252f20140221065614-542310758.jpg%26ehk%3dqK%252fZD9FhpHzXVxK7C8h%252bwqnDYYvZcZz%252fhUK1%252bN6gxIA%253d%26risl%3d%26pid%3dImgRaw%26r%3d0&exph=526&expw=372&q=%e8%b7%af%e4%ba%ba%e4%b8%89%e5%9b%bd&simid=608050804270576178&FORM=IRPRST&ck=C5AFA74321B5D03CBFB14A425D947101&selectedIndex=35",
            "sec-ch-ua": "\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
            "sec-ch-ua-arch": "\"x86\"",
            "sec-ch-ua-bitness": "\"64\"",
            "sec-ch-ua-full-version": "\"117.0.5938.149\"",
            "sec-ch-ua-full-version-list": "\"Google Chrome\";v=\"117.0.5938.149\", \"Not;A=Brand\";v=\"8.0.0.0\", \"Chromium\";v=\"117.0.5938.149\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-model": "\"\"",
            "sec-ch-ua-platform": "\"Linux\"",
            "sec-ch-ua-platform-version": "\"5.15.0\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
        }
        cookies = {
            "_IDET": "MIExp=0&VSNoti2=20230905",
            "MUID": "22AC14C3CC7161BE35F307BDCDB16074",
            "SRCHD": "AF=NOFORM",
            "SRCHUID": "V=2&GUID=509CD84DDDEE451B81F353352EC11217&dmnchg=1",
            "_UR": "QS=0&TQS=0",
            "MMCASM": "ID=C97FA632CD7D49E6B6BA5A611262424E",
            "MUIDB": "22AC14C3CC7161BE35F307BDCDB16074",
            "MicrosoftApplicationsTelemetryDeviceId": "ac12ae6c-86b1-4161-b6d7-b9f1cc614917",
            "ABDEF": "V=13&ABDV=13&MRNB=1694482647722&MRB=0",
            "_Rwho": "u=d",
            "_EDGE_S": "SID=3874A986B86B6D053163BA28B9286C93&ui=zh-cn",
            "ENSEARCH": "BENVER=0",
            "USRLOC": "HS=1&ELOC=LAT=29.873886108398438|LON=121.55025482177734|N=%E5%AE%81%E6%B3%A2%EF%BC%8C%E6%B5%99%E6%B1%9F%E7%9C%81|ELT=1|",
            "ipv6": "hit=1697775456071&t=4",
            "SRCHUSR": "DOB=20230905&T=1697771855000&TPC=1697771956000",
            "SNRHOP": "I=&TS=",
            "_HPVN": "CS=eyJQbiI6eyJDbiI6NCwiU3QiOjAsIlFzIjowLCJQcm9kIjoiUCJ9LCJTYyI6eyJDbiI6NCwiU3QiOjAsIlFzIjowLCJQcm9kIjoiSCJ9LCJReiI6eyJDbiI6NCwiU3QiOjAsIlFzIjowLCJQcm9kIjoiVCJ9LCJBcCI6dHJ1ZSwiTXV0ZSI6dHJ1ZSwiTGFkIjoiMjAyMy0xMC0yMFQwMDowMDowMFoiLCJJb3RkIjowLCJHd2IiOjAsIkRmdCI6bnVsbCwiTXZzIjowLCJGbHQiOjAsIkltcCI6MjV9",
            "ai_session": "S1yJDxIU1l1S70+s0//aX1|1697771857076|1697772162430",
            "_RwBf": "r=0&ilt=36&ihpd=2&ispd=2&rc=75&rb=0&gb=0&rg=200&pc=72&mtu=0&rbb=0&g=0&cid=&clo=0&v=8&l=2023-10-19T07:00:00.0000000Z&lft=0001-01-01T00:00:00.0000000&aof=0&o=2&p=&c=&t=0&s=0001-01-01T00:00:00.0000000+00:00&ts=2023-10-20T03:25:36.6657608+00:00&rwred=0&wls=&wlb=&lka=0&lkt=0&TH=&aad=0",
            "_SS": "SID=3874A986B86B6D053163BA28B9286C93&R=75&RB=0&GB=0&RG=200&RP=72",
            "SRCHHPGUSR": "SRCHLANG=zh-Hans&BRW=NOTP&BRH=M&CW=759&CH=944&SCW=759&SCH=944&DPR=1.0&UTC=480&DM=0&PV=5.15.0&PRVCW=759&PRVCH=944&HV=1697772344&WTS=63833368754&IG=3855D085AD64442A982F632B5E62A1BB&BZA=0"
        }
        url = "https://cn.bing.com/images/api/custom/search"
        params = {
            "q": key,
            "count": "25",
            "offset": "45",
            "skey": "DlmGQN03PgnnhpJzzDgrAWYXuI_jf_iANht2gGTV7bo",
            "safeSearch": "Strict",
            "mkt": "zh-cn",
            "setLang": "zh-cn",
            "IG": "DD6AAF770FD5442AA3620FEECDBD3E66",
            "IID": "idpfs",
            "SFX": str(pagesize)
        }
        response = requests.get(url, headers=headers, cookies=cookies, params=params)
        value=response.json()['value']
        url_list = [i['contentUrl'] for i in value]
        return url_list
    except BaseException as e:
        print("search 函数出错啦，大概率需要更换cookies，去这个接口换一个叭 https://cn.bing.com/images/search?q=1&form=HDRSC2&first=1")

